<template>
  <div class="createPost-container">
    <el-form
      ref="postForm"
      :model="postForm"
      label-position="right"
      class="form-container"
    >
      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.status">
        <el-button
          style="margin-left: 10px"
          type="success"
          @click="submit"
        >发布</el-button>
        <router-link :to="'/branch/index'">
          <el-button style="margin-left: 10px" type="success">返回</el-button>
        </router-link>
      </sticky>

      <div class="createPost-main-container">
        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="项目名称:"
        >
          <el-input
            v-model="postForm.project_name"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入项目名称"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="开标时间:"
        >
          <el-date-picker
            v-model="postForm.bid_open_time"
            value-format="yyyy-MM-dd"
            type="date"
            class="article-textarea"
            :disabled="true"
            placeholder="选择日期"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="详细地址:"
        >
          <el-input
            v-model="postForm.address_detailed"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入详细地址"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="资格条件:"
        >
          <el-input
            v-model="postForm.qualifications"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入资格条件"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="人员要求:"
        >
          <el-input
            v-model="postForm.researchers"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入人员要求"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="建设单位:"
        >
          <el-input
            v-model="postForm.construction_unit"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入建设单位"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="代理单位:"
        >
          <el-input
            v-model="postForm.agent_unit"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入代理单位"
          />
        </el-form-item>
        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="项目概况:"
        >
          <el-input
            v-model="postForm.project_overview"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入项目概况"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="分值组成:"
        >
          <el-input
            v-model="postForm.scores"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入分值组成"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="资信分情况:"
        >
          <el-input
            v-model="postForm.credit_points"
            :rows="1"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            autosize
            placeholder="请输入资信分情况"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 10px"
          label-width="120px"
          label="商务标评分情况:"
        >
          <el-input
            v-model="postForm.business_standard"
            type="textarea"
            class="article-textarea"
            :disabled="true"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入商务标评分情况"
          />
        </el-form-item>

        <el-divider
          style="margin:20px;"
        ><span
          style="color:#666;font-size:12px;"
        >以上为当前项目审核信息</span></el-divider>
        <el-form
          ref="submitFrom"
          :model="submitFrom"
          label-position="right"
          class="form-container"
        >
          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务负责人:"
          >
            <el-input
              v-model="submitFrom.business_director"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="不可输入数字"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务负责人电话:"
          >
            <el-input
              v-model="submitFrom.director_mobile"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="请输入正确的业务负责人电话"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="工程费用:"
          >
            <el-input
              v-model="submitFrom.engineering_cost"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="最高限价:"
          >
            <el-input
              v-model="submitFrom.price_ceiling"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="中标价:"
          >
            <el-input
              v-model="submitFrom.price_tag"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="业务类型:"
          >
            <el-input
              v-model="submitFrom.business_type"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="投标描述:"
          >
            <el-input
              v-model="submitFrom.tender_describe"
              :rows="1"
              type="textarea"
              class="article-textarea"
              :disabled="true"
              autosize
              placeholder="格式:100.00"
            />
          </el-form-item>

          <el-form-item
            style="margin-bottom: 10px"
            label-width="120px"
            label="附件下载:"
          >
            <ul style="padding:5px">
              <!-- <li
                v-for="(item, index) in file_list"
                :key="index"
                style="list-style :none"
                @click="downLoad(item.url, item.name)"
              > -->
              <viewer
                ref="viewer"
                :images="images"
                class="viewer"
                style="display:flex;flex-direction:row"
                @inited="inited"
              >
                <div
                  v-for="(src, index) in file_list"
                  :key="index"
                  style="width:80px;height:90;margin:0px 20px 0px 0px;"
                >
                  <img
                    :src="src.url"
                    class="image"
                    width="80"
                    height="80"
                    alt="该资源无图片"
                    onerror="this.src='http://dc.m.tuobao.com/enclosure/none.png';this.onerror=null"
                  >
                  <span
                    :title="src.name"
                    style="white-space:nowrap;text-overflow:ellipsis;width:100%;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:2;display:-webkit-box;  "
                    @click="downLoad(src.url, src.name)"
                  >
                    {{ src.name }}
                  </span>
                </div>
              </viewer>
              <!-- </li> -->
              <span
                style="width:100%;text-align:center;color:#ccc"
              >(点击文件名下载)</span>
            </ul>
          </el-form-item>
        </el-form>
        <el-divider><span
          style="color:#666;font-size:12px;"
        >以上为当前项目投标信息</span></el-divider>
        <el-form-item
          style="margin-bottom: 10px;margin-top:30px"
          label-width="120px"
          label="签约描述:"
        >
          <el-input
            v-model="submitFrom.signing_describe"
            :rows="1"
            type="textarea"
            autosize
            placeholder="格式:100.00"
          />
        </el-form-item>

        <el-form-item
          style="margin-bottom: 30px;margin-top:10px"
          label-width="120px"
          label="文件上传:"
        >
          <el-upload
            ref="upload"
            class="upload-demo"
            :action="uploadAction"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="getFilePath"
            multiple
            :limit="5"
            :on-exceed="handleExceed"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky' // 粘性header组件
import VDistpicker from 'v-distpicker'
import { getProjectDetail, insertFile, insertSigningInfo } from '@/api/project'
import { getToken } from '@/utils/auth'
import { getInfo } from '@/api/user'
import { downloadFile } from '@/utils/request'

const defaultForm = {
  status: 'draft',
  // projectArea: { province: "广东省", city: "广州市", area: "海珠区" }, //项目区域
  address_detailed: '', // 详细地址
  projectAreaStr: '', // 选择的地区字符串
  bid_open_time: '', // 开标时间
  project_name: '', // 项目名称
  qualifications: '', // 资格条件
  researchers: '', // 人员要求
  constructionUnit: '', // 建设单位
  agentUnit: '', // 代理单位
  projectOverview: '', // 项目概况
  scores: '', // 分值组成
  business_standard: '', // 商务标评分情况
  credit_points: '' // 资信分情况
}
// 需要的表单
const formSubmit = {
  business_director: '', // 业务负责人
  director_mobile: '', // 业务负责人电话
  engineering_cost: '', // 工程费用
  price_ceiling: '', // 最高限价
  price_tag: '', // 中标价
  business_type: '', // 业务类型
  tender_describe: '', // 投标描述
  signing_describe: '', // 签约描述
  file_path: '', // 上传文件后服务器返回的路片名称(投标附件)
  signing_path: [] // 上传文件后服务器返回的路片名称(签约附件)
}
// http://dc.tuobao.m.com/storage/images
export default {
  name: 'ArticleDetail',
  components: {
    Sticky,
    VDistpicker
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      id: '', // 当前项目的id
      postForm: Object.assign({}, defaultForm),
      submitFrom: Object.assign({}, formSubmit),
      images: [],
      file_list: [],
      signing_paths: [], // 签约附件数组
      options: [
        {
          value: '监理',
          label: '监理'
        },
        {
          value: '造价咨询',
          label: '造价咨询'
        },
        {
          value: '代理',
          label: '代理'
        },
        {
          value: '全过程咨询',
          label: '全过程咨询'
        }
      ],
      // 添加头部请求
      headers: {
        Authorization: 'Bearer ' + getToken(),

        post: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      },
      uploadAction: '/adminapi/project/insertFile'
    }
  },
  computed: {},
  created() {
    if (this.isEdit) {
      this.id = this.$route.params && this.$route.params.id
      this.getProjectData(this.id)
    }
    this.getUserInfo()
    this.submitFrom.signing_path = []
  },
  methods: {
    inited(viewer) {
      this.$viewer = viewer
    },
    // 下载文件
    downLoad(url, name) {
      // console.log(1);
      // console.log(url);
      const x = new XMLHttpRequest()
      x.open('GET', url, true)
      x.responseType = 'blob'
      x.onload = function() {
        const url = window.URL.createObjectURL(x.response)
        const a = document.createElement('a')
        a.href = url
        a.download = name
        a.click()
      }
      x.send()
    },
    // 获取文件返回路径
    getFilePath(res) {
      // console.log(res.data);
      var that = this
      that.signing_paths.push(res.data)
      this.submitFrom.signing_path = that.signing_paths
    },
    // 获取项目
    getProjectData(id) {
      new Promise((reslove, reject) => {
        getProjectDetail({ id: id }).then(response => {
          // console.log(response.data);
          this.postForm = response.data
          this.submitFrom = response.data
          // console.log("submit");
          // console.log(this.submitFrom);
          for (let i = 0; i < this.submitFrom.file_path.length; i++) {
            if (
              this.submitFrom.file_path[i].fileType == 'png' ||
              this.submitFrom.file_path[i].fileType == 'jpeg' ||
              this.submitFrom.file_path[i].fileType == 'jpg'
            ) {
              this.images.push(this.submitFrom.file_path[i].file_path)
              this.file_list.push({
                name: this.submitFrom.file_path[i].file_name,
                url: this.submitFrom.file_path[i].file_path
              })
            } else {
              // this.file_list.push({
              //   name: this.submitFrom.file_path[i].file_name,
              //   url: "http://dc.tuobao.m.com/enclosure/none.png"
              // });
              // this.images.push("http://dc.tuobao.m.com/enclosure/none.png");
              this.file_list.push({
                name: this.submitFrom.file_path[i].file_name,
                url: this.submitFrom.file_path[i].file_path
              })
              this.images.push('http://dc.m.tuobao.com/enclosure/none.png')
            }
          }
          // console.log(this.file_list);
        })
      })
    },
    // 发布
    submit() {
      new Promise((resolve, reject) => {
        insertSigningInfo({
          id: this.id,
          submitFrom: this.submitFrom,
          username: this.username
        }).then(() => {
          this.$message({
            type: 'success',
            message: '签约信息填写成功!'
          })
          this.$router.push('/branch/index')
        })
      })
    },
    // 文件预览
    handlePreview() {},
    // 文件移除
    handleRemove(file, fileList) {
      for (let i = 0; i < this.submitFrom.signing_path.length; i++) {
        if (this.submitFrom.signing_path[i].name == file.name) {
          this.submitFrom.signing_path.splice(i, 1)
          return
        }
      }
    },
    // 文件限制
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    // 文件移除前
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 获取当前用户的登录信息
    getUserInfo() {
      new Promise((resolve, reject) => {
        getInfo().then(response => {
          // console.log("info");
          // console.log(response.data);
          this.username = response.data.name // 当前登录用户的姓名
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.el-form-item {
  margin: 0px;
}
.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 0px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      text-align: center;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  .el-textarea__inner {
    background-color: white;
    color: #000;
    opacity: 0.7;
  }
  .el-input__inner {
    background-color: white;
    color: #000;
    opacity: 0.7;
  }
}
</style>
